import * as actions from '../actions/index';

// 页面初始化数据
function getInitialState(pageInfo) {
  const { resourceSubject } = pageInfo;
  const { path, subject, revision } = resourceSubject;
  const isShowCommitDetail = subject === 'commit' && !!revision;
  // 页面初始化数据
  const initialState = {
    commits: [],
    curCommitInfo: {},
    isLoadingList: false,
    filterText: '',
    isShowCommitDetail,
    curCommitId: revision || '',
    isLoading: false,
    resourcePath: path || '',
  };
  return initialState;
}

const defaultAction = {
  type: 'doNothing',
};

export default function index(state = getInitialState(window.FORCE), action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.UPDATE_PAGE_INFO:
      return getInitialState(action.data);
    default:
      return state;
  }
}
